
import { Component, OnInit, Injector, Input, ViewChild, AfterViewInit } from '@angular/core';
import { ModalComponentBase } from '@shared/component-base/modal-component-base';
import {
  CreateOrUpdateStockCategoryInput,
  SelectListItem,
  StockCategoryEditDto,
  StockCategoryServiceProxy
} from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';

@Component({
  selector: 'create-or-edit-stock-category',
  templateUrl: './create-or-edit-stock-category.component.html',
  styleUrls: [
    'create-or-edit-stock-category.component.less'
  ],
})

export class CreateOrEditStockCategoryComponent
  extends ModalComponentBase
  implements OnInit {
  /**
  * 编辑时DTO的id
  */
  id: any;

  entity: StockCategoryEditDto = new StockCategoryEditDto();

  collegeList: Array<SelectListItem>;

  statusList: Array<SelectListItem>;
  /**
  * 构造函数，在此处配置依赖注入
  */
  constructor(
    injector: Injector,
    private _stockCategoryService: StockCategoryServiceProxy

  ) {
    super(injector);
  }

  ngOnInit(): void {
    this.init();
  }


  /**
  * 初始化方法
  */
  init(): void {
    this._stockCategoryService.getForEdit(this.id).subscribe(result => {
      this.entity = result.stockCategory;
      this.collegeList = result.collegeList;
      this.statusList=result.statusList;
    });
  }


  /**
  * 保存方法,提交form表单
  */
  submitForm(): void {
    const input = new CreateOrUpdateStockCategoryInput();
    input.stockCategory = this.entity;

    this.saving = true;

    this._stockCategoryService.createOrUpdate(input)
      .pipe(finalize(() => (this.saving = false)))
      .subscribe(() => {
        this.notify.success(this.l('SavedSuccessfully'));
        this.success(true);
      });
  }
}
